package main

import (
	"fmt"
	"sort"
)

func main() {
	//mapInit()
	//mapExercise()
	//mapCRUD()
	//mapForRange()
	//mapSlice()
	mapSort()
}

func mapInit() {
	var m1 map[string]string
	var m2 = make(map[string]int, 10)
	var m3 = map[int]string{
		1: "v1", 2: "v2",
	}
	var m4 map[int]int
	// 在使用map之前，需要先make
	m1 = make(map[string]string, 10)
	m1["no1"] = "no1"
	m1["no2"] = "no2"
	m1["no2"] = "no3"
	m1["no3"] = "no1"
	m2["no1"] = 1
	m2["no2"] = 2
	fmt.Println(m1, m2, m3, m4)
}

func mapExercise() {
	// 存放3个学生信息，每个学生又name和sex的信息
	var students = make(map[string]map[string]string, 3)
	students["stu01"] = make(map[string]string, 2)
	students["stu01"]["name"] = "stu01"
	students["stu01"]["sex"] = "男"
	students["stu02"] = make(map[string]string, 2)
	students["stu02"]["name"] = "stu02"
	students["stu02"]["sex"] = "女"
	students["stu03"] = make(map[string]string, 2)
	students["stu03"]["name"] = "stu02"
	students["stu03"]["sex"] = "男"
	fmt.Println(students)
}

func mapAdd() {
	var m1 map[string]string
	m1 = make(map[string]string, 10)

	// 因为no1, no2不存在，所以相当于增加
	m1["no1"] = "no1"
	m1["no2"] = "no2"
	fmt.Println("add", m1)
}

func mapModify() {
	var m1 map[string]string
	m1 = make(map[string]string, 10)
	m1["no1"] = "no1"

	// 因为no2已存在，所以相当于修改
	m1["no1"] = "no1.1"
	fmt.Println("modify", m1)
}

func mapDel() {
	var m1 map[string]string
	m1 = make(map[string]string, 10)

	m1["no1"] = "no1"
	m1["no2"] = "no2"

	// 如果m1为nil或者no2不在m1中，则不执行，不会报错
	delete(m1, "no2")
	delete(m1, "no3")
	fmt.Println("delete1", m1)
	delete(m1, "no1")
	delete(m1, "no1")
	delete(m1, "no1")
	fmt.Println("delete2", m1)

	// 如果想要删除所有的元素
	// 1、遍历所有的key并逐个删除
	// 2、直接make一个新的空间
}

func mapFind() {
	var m1 map[string]string
	m1 = make(map[string]string, 10)

	m1["no1"] = "no1"
	m1["no2"] = "no2"

	//查找
	m1["no1"] = "no1"
	m1["no2"] = "no2"
	var key = "no3"
	val, exist := m1[key]
	if exist {
		fmt.Printf("key [%s] exist, value = %s\n", key, val)
	} else {
		fmt.Printf("key [%s] not exist", key)
	}
}

func mapCRUD() {
	//mapAdd()
	//mapModify()
	//mapDel()
	mapFind()
}

func mapForRange() {
	var m1 map[string]string
	m1 = make(map[string]string, 10)

	m1["no1"] = "no1"
	m1["no2"] = "no2"
	m1["no3"] = "no3"

	for k, v := range m1 {
		fmt.Printf("m1[%s]=%s\n", k, v)
	}

	fmt.Printf("m1有%d对kv键值对", len(m1))
}

func mapSlice() {
	var students []map[string]string
	students = make([]map[string]string, 1)
	if students[0] == nil {
		students[0] = make(map[string]string, 2)
		students[0]["name"] = "tom"
		students[0]["age"] = "15"
	}
	// 下面这样写会数组越界
	//if students[1] == nil {
	//	students[1] = make(map[string]string, 2)
	//	students[1]["name"] = "jack"
	//	students[1]["age"] = "14"
	//}

	// 正确写法应该用append
	// 1、定义student
	student := map[string]string{
		"name": "jack",
		"age":  "14",
	}
	// 2、append进去
	students = append(students, student)
	fmt.Println(students)
}

func mapSort() {
	map1 := map[int]int{
		1:  1,
		2:  2,
		3:  3,
		4:  4,
		5:  15,
		10: 1105,
		11: 1115,
		12: 1125,
	}
	fmt.Println(map1)
	var slice []int
	for k, _ := range map1 {
		slice = append(slice, k)
	}
	sort.Ints(slice)
	fmt.Println(slice)

	for _, k := range slice {
		fmt.Printf("map [%v]=%v\n", k, map1[k])
	}
}
